//
//  RYCHTTPRequestModel.h
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequestDelegate.h"

//
//RYCHTTPRequestModelDelegate
//
@class RYCHTTPRequestModel;
@protocol RYCHTTPRequestModelDelegate <NSObject>
@optional
- (void)didHTTPRequestModelFinished:(RYCHTTPRequestModel*)aModel;
- (void)didHTTPRequestModelFailed:(RYCHTTPRequestModel*)aModel;
@end

//
//RYCHTTPRequestModel
//http请求模型基类,负责发送http请求,解析响应数据(供UI使用)
//
typedef enum 
{
    RYCHTTPRequestModelState_Pending,
    RYCHTTPRequestModelState_Loading,
    RYCHTTPRequestModelState_Finished,
    RYCHTTPRequestModelState_Failed,
}RYCHTTPRequestModelState;

typedef enum
{
    RYCHTTPRequestModelOperation_Normal,
    RYCHTTPRequestModelOperation_Refresh,
    RYCHTTPRequestModelOperation_Forward,//向下获取，获取更多
    RYCHTTPRequestModelOperation_Backward//向上获取
}RYCHTTPRequestModelOperation;

//
//RYCHTTPRequestModel
//http请求模型基类,负责发送http请求,解析响应数据(供UI使用).
//
@class RYCHTTPRequestQueue;
typedef NSObject RYCHTTPRequestResponseObject;
@interface RYCHTTPRequestModel : NSObject <ASIHTTPRequestDelegate>
{
    @private
    id<RYCHTTPRequestModelDelegate> _delegate;
    RYCHTTPRequestModelState _state;
    RYCHTTPRequestModelOperation _operation;
    
    ASIHTTPRequest* _request;
    RYCHTTPRequestQueue* _requestQueue;
    
    RYCHTTPRequestResponseObject* _responseObject;
    NSError* _error;
}

@property (nonatomic, assign) id<RYCHTTPRequestModelDelegate> delegate;
@property (nonatomic) RYCHTTPRequestModelOperation operation;
@property (nonatomic, readonly, retain) ASIHTTPRequest* httpRequest;
@property (nonatomic, retain) RYCHTTPRequestQueue* requestQueue;//如不设置,将使用默认队列发送请求

@property (nonatomic, retain) RYCHTTPRequestResponseObject* responseObject;//解析完响应数据后对应的具体业务数据类型
@property (nonatomic, retain) NSError* error;

- (id)initWithDelegate:(id<RYCHTTPRequestModelDelegate>)delegate;
- (void)sendRequest;
- (void)cancelRequest;

//------state---------
- (BOOL)isDone;//finished or failed
- (BOOL)isFinished;
- (BOOL)isFailed;
- (BOOL)isLoading;

//-----子类重载---------
- (ASIHTTPRequest*)request;//default is noop
- (BOOL)handleResponseString:(id)aResponseString;//default return YES.返回NO,将视作request failed.
- (void)reset;
@end

/**
    NSError
*/
@interface NSError (ASIError)
- (NSError*)convertToCurrentLanguage;//转换成当前客户端语言版本，目前只支持中文
@end
